package org.one.stone.soup.grfxML;

import java.util.Vector;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataRPolygon extends DataGeometry implements ITag {
	public static String DEFAULT_ID = "_RPolygon";
		DataSize radius = new DataSize(20);
		DataSize radiusView = new DataSize(20);
		DataSize sides = new DataSize(7);

		int lastSides;
		int lastRadius;

		DataPoints _points = new DataPoints();
		DataPoints _pointsView = new DataPoints();
		GeometryPolygon _polygon = null;
		DataTranslation _translation;
public DataRPolygon() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.RADIUS))
		{
			radius = new DataSize(TagModifier.SIZE_TYPE_SIZE,10);
	  		radius.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.SIDES))
		{
			sides = new DataSize(TagModifier.SIZE_TYPE_COUNT,7);
			sides.buildFromgrfxML(element);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.RPOLYGON,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.RADIUS))
		{
	  		radius.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.SIDES))
		{
			sides.copyFromgrfxML(element);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.RPOLYGON,""+element.getStartLineNo()} );
		}
	}
}
public DataGeometry getClone() {


	DataRPolygon newRPolygon = new DataRPolygon();





	Vector elements = getClonedElements();
	Vector newElements = newRPolygon.getClonedElements();
	for(int loop=0;loop<elements.size();loop+=2)
	{
		Field oldObj = (Field)newElements.elementAt(loop);
		Field newObj = (Field)elements.elementAt(loop+1);
		newRPolygon.replace(oldObj,newObj);
	}



	return newRPolygon;
}
public DataGeometry getClone(String path) {

	DataRPolygon newRPolygon = new DataRPolygon();

	GrfxMLStoreProcessor.setCloneName( path,this,newRPolygon );

	super.getClonedElements(path,newRPolygon);

	newRPolygon.radius = radius.getClone(path);
	newRPolygon.sides = sides.getClone(path);

	return newRPolygon;
}
public Geometry getGeometry(GrfxMLEngine engine)
{
	if(_polygon==null && _translation!=null && color!=null)
	{
		_polygon = new GeometryPolygon(
				engine,
				_translation.window,
				bounds,
				_translation.visibleView,
				transparency,
				color,
				filled,
				DataState.TRUE,
				_translation.positionView,
				offsetView,
				_pointsView,
				new DataSize(1));
		if( !NameHelper.isSystemName(getId()) )
		{
			_polygon.setId(getId());
		}
	}

	if(sides.getValue()!=lastSides || radius.getValue()!=lastRadius)
	{
		lastSides = sides.getValue();
		lastRadius = radius.getValue();
		GeometryProcessor.process(this);
	}

	return(_polygon);
}
	public java.awt.Polygon getPolygon() {
	if(_polygon==null)
	{
		return null;
	}
	return _polygon.polygon;
}
public DataSize getRadius() {
	return radius;
}
public DataSize getSides() {
	return sides;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(radius,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(sides,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	_polygon=null;

	super.replace(oldObj,newObj);

	if(radius==oldObj)
		radius=(DataSize)newObj;

	if(sides==oldObj)
		sides=(DataSize)newObj;

	if(_points==oldObj)
		_points=(DataPoints)newObj;

	if(recursive)
	{
		_points.replace(oldObj,newObj);
	}

}
public void resetChanged() {
	super.resetChanged();
	radius.resetChanged();
	radiusView.resetChanged();
	sides.resetChanged();
	_points.resetChanged();
	_pointsView.resetChanged();
}
protected void setPoints(DataPoints newPoints) {
	_points = newPoints;
}
protected void setRadius(DataSize newRadius) {
	radius = newRadius;
}
protected void setSides(DataSize newSides) {
	sides = newSides;
}
protected void setTranslation(DataTranslation newTranslation) {
	_translation = newTranslation;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += radius.togrfxML(new DataSize(20),Token.RADIUS);
	xml += sides.togrfxML(new DataSize(7),Token.SIDES);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t)
{

	if(_translation==null)
		_translation=t;

	try{
		GrfxMLProcessor.translate(_points,TagModifier.POINT_TYPE_POINT,t,_pointsView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	super.translate(t);
}
}
